package com.genscript.ms.b.core.config;

import org.hibernate.validator.HibernateValidator;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

import javax.validation.Validator;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class ValidatorConfig {
    @Bean
    public Validator validator(MessageSource messageSource) {
        LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
        localValidatorFactoryBean.setProviderClass(HibernateValidator.class);
        localValidatorFactoryBean.setValidationMessageSource(messageSource);
        /* 设置validator模式为快速失败返回 */
        Map<String, String> validationProperties = new HashMap<>(1);
        validationProperties.put("hibernate.validator.fail_fast", "true");
        localValidatorFactoryBean.setValidationPropertyMap(validationProperties);
        return localValidatorFactoryBean;
    }
}
